package org.awa.demo.opencv.constant;

import org.geotools.geometry.jts.ReferencedEnvelope;
import org.geotools.referencing.CRS;
import org.opengis.geometry.Envelope;
import org.opengis.referencing.crs.CoordinateReferenceSystem;

/**
 * @author xxf
 */
public class CRSUtil {
    /*
    * EPSG官网： https://epsg.io/
    * 可查询坐标系内容
    * 通过getCrs获取编码，去epsg官网查询坐标系参数
    * geotools使用 OGC WKT数据做定制坐标系转换
    *
    * CoordinateReferenceSystem crs = CRS.parseWKT(wkt);
    *
    * */

    static 	int[] codes={
            //WGS 84
            3857,
            //CGCS2000
            4512, 4511, 4510, 4509, 4508, 4507, 4506, 4505, 4504, 4503, 4502, 4501, 4500, 4499, 4498, 4497, 4496, 4495, 4494, 4493, 4492, 4491,
            4554, 4553, 4552, 4551, 4550, 4549, 4548, 4547, 4546, 4545, 4544, 4543, 4542, 4541, 4540, 4539, 4538, 4537, 4536, 4535, 4534,
            4533, 4532, 4531, 4530, 4529, 4528, 4527, 4526, 4525, 4524, 4523, 4522, 4521, 4520, 4519, 4518, 4517, 4516, 4515, 4514, 4513,
            //Beijing 1954
            21463, 21462, 21461, 21460, 21459, 21458, 21457, 21456, 21455, 21454, 21453,
            21423, 21422, 21421, 21420, 21419, 21418, 21417, 21416, 21415, 21414, 21413,
            2442, 2441, 2440, 2439, 2438, 2437, 2436, 2435, 2434, 2433, 2432, 2431, 2430, 2429, 2428, 2427, 2426, 2425, 2424, 2423, 2422,
            2421, 2420, 2419, 2418, 2417, 2416, 2415, 2414, 2413, 2412, 2411, 2410, 2409, 2408, 2407, 2406, 2405, 2404, 2403, 2402, 2401,
            //New Beijing
            4589, 4588, 4587, 4586, 4585, 4584, 4583, 4582, 4581, 4580, 4579,
            4578, 4577, 4576, 4575, 4574, 4573, 4572, 4571, 4570, 4569, 4568,
            4822, 4812, 4800, 4799, 4798, 4797, 4796, 4795, 4794, 4793, 4792, 4791, 4790, 4789, 4788, 4787, 4786, 4785, 4784, 4783, 4782,
            4781, 4780, 4779, 4778, 4777, 4776, 4775, 4774, 4773, 4772, 4771, 4770, 4769, 4768, 4767, 4766, 4756, 4755, 4754, 4753, 4753,
            //Xian 80
            2348, 2347, 2346, 2345, 2344, 2343, 2342, 2341, 2340, 2339, 2338,
            2337, 2336, 2335, 2334, 2333, 2332, 2331, 2330, 2329, 2328, 2327,
            2390, 2389, 2388, 2387, 2386, 2385, 2384, 2383, 2382, 2381, 2380, 2379, 2378, 2377, 2376, 2375, 2374, 2373, 2372, 2371, 2370,
            2369, 2368, 2367, 2366, 2365, 2364, 2363, 2362, 2361, 2360, 2359, 2358, 2357, 2356, 2355, 2354, 2353, 2352, 2351, 2350, 2349,
    };


    /**
     * 方法描述: 获取投影坐标系
     *
     */
    public static String getCRS(double x, double y,String crsName) {
        for (int code : codes) {
            try {
                String epsgCode="EPSG:"+code;
                CoordinateReferenceSystem crs = CRS.decode(epsgCode);
                Envelope envelope=CRS.getEnvelope(crs);
                ReferencedEnvelope projectedBounds = new ReferencedEnvelope(envelope.getMinimum(1),envelope.getMaximum(1),envelope.getMinimum(0),envelope.getMaximum(0),crs);
                if(projectedBounds.contains(x,y)){
                    String nameCode=crs.getName().getCode();
                    System.out.println("[INFO]nameCode:"+nameCode);
                    if(nameCode.startsWith(crsName)){
                        return epsgCode;
                    }
                }
            }catch (Exception e){
                System.out.println("[ERROR]"+e.toString());
                e.printStackTrace();
            }
        }
        return null;
    }

    public static void main(String[] args) {
        String code = getCRS(40514474.2589528,3356418.54543048,CRSConstant.CGC2000);
        System.out.println("code >>> "+code);
    }
}
